package com.runa.protocol.dto;

import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.enums.ModbusDataType;
import com.runa.protocol.enums.S7Enum;

import java.math.BigDecimal;

public class S7Point extends PlcPoint implements IPoint {

    private S7Enum.Area area;

    private int dbNumber;

    private int address;

    private Integer deviation;

    private Object value;

    public S7Point(Integer id, String code, ModbusDataType dataType, S7Enum.Area area, int dbNumber, int address, ByteOrder byteOrder, Integer deviation) {
        this.id = id;
        this.code = code;
        this.dataType = dataType;
        this.area = area;
        this.dbNumber = dbNumber;
        this.address = address;
        this.byteOrder = byteOrder;
        this.deviation = deviation;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public ModbusDataType getDataType() {
        return dataType;
    }

    public void setDataType(ModbusDataType dataType) {
        this.dataType = dataType;
    }

    public S7Enum.Area getArea() {
        return area;
    }

    public void setArea(S7Enum.Area area) {
        this.area = area;
    }

    public Integer getDbNumber() {
        return dbNumber;
    }

    public void setDbNumber(Integer dbNumber) {
        this.dbNumber = dbNumber;
    }

    public Integer getAddress() {
        return address;
    }

    public void setAddress(Integer address) {
        this.address = address;
    }

    public Integer getDeviation() {
        return deviation;
    }

    public void setDeviation(Integer deviation) {
        this.deviation = deviation;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public ByteOrder getByteOrder() {
        return byteOrder;
    }

    public void setByteOrder(ByteOrder byteOrder) {
        this.byteOrder = byteOrder;
    }

    public void constructValue(Object value) {
        if(value == null) {
            return;
        }
        Class<?> valueClass = value.getClass();
        if(valueClass == Boolean.class) {
            this.value = value;
        } else if(valueClass == BigDecimal.class){
            BigDecimal decimal = (BigDecimal) value;
            switch (dataType) {
                case Modbus_Int8:
                case Modbus_UInt8:
                    this.value = (byte) decimal.intValue();
                    break;
                case Modbus_Int16:
                case Modbus_UInt16:
                    this.value = decimal.shortValue();
                    break;
                case Modbus_Int32:
                case Modbus_UInt32:
                    this.value = decimal.intValue();
                    break;
                case Modbus_Float:
                    this.value = decimal.floatValue();
                    break;
                case Modbus_Double:
                    this.value = decimal.doubleValue();
                    break;
                default:
            }
        }
    }
}
